<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="综合评分" prop="star">
        <el-input
          v-model="queryParams.star"
          placeholder="请输入"
          type="number"
          style="width: 150px"
          max="5"
          min="0"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评级时间">
        <el-date-picker
          v-model="daterangeCommentTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:evaluate:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:evaluate:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:evaluate:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:evaluate:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="evaluateList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单评价" align="center" prop="id" width="100" />
      <el-table-column label="用户" align="center" prop="userId" width="100">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title="用户信息"
            width="300"
            trigger="click"
          >
            <div>{{ scope.row.userId }}</div>
            <div type="primary" class="hidden" slot="reference">
              {{ scope.row.userId }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="订单" align="center" prop="orderId" width="100">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title="订单信息"
            width="300"
            trigger="click"
          >
            <div v-if="scope.row.order != null">
              <div>订单id：{{ scope.row.order.number }}</div>
              <div>门店id：{{ scope.row.order.shopId }}</div>
              <div>车辆id:{{ scope.row.order.carId }}</div>
              <div>服务类别:<dict-tag :options="dict.type.service_type" :value="scope.row.order.serviceId"/>
              </div>
              <div>
                <span v-if="scope.row.order.couponId != null"
                  >优惠卷Id：{{ scope.row.order.couponId }}</span
                >
                <span v-else>未使用优惠卷</span>
              </div>
              <div>总价: {{ scope.row.order.totalPrice }}</div>
              <div>实付价格:{{ scope.row.order.realPrice }}</div>
              <div>核销码:{{ scope.row.order.verificationCode }}</div>
              <div>
                <span v-if="scope.row.order.verificationTime != null"
                  >核销时间:{{ scope.row.order.verificationTime }}</span
                >
                <span>未核销</span>
              </div>
              <div>预约时间:{{ scope.row.order.subscribeTime }}</div>
              <div class="flex">
                <span>状态：</span>
                <div><dict-tag
                  :options="dict.type.order_status"
                  :value="scope.row.order.state"
                /></div>
              </div>
            </div>
            <div type="primary" class="hidden" slot="reference">
              {{ scope.row.orderId }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="综合评分" align="center" prop="star" width="200">
        <template slot-scope="scope">
          <el-rate :value="scope.row.star" :colors="colors" disabled> </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评语" align="center" prop="comments" width="150">
        <template slot-scope="scope">
          <el-popover placement="left" title="评论" width="300" trigger="click">
            <div>{{ scope.row.comments }}</div>
            <div type="primary" class="hidden" slot="reference">
              {{ scope.row.comments }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="评价照片"
        align="center"
        prop="imgUrl"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            v-if="
              scope.row.imgItemList != null && scope.row.imgItemList.length > 0
            "
            :src="scope.row.imgItemList[0].photo"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="评级时间"
        align="center"
        prop="commentTime"
        width="180"
      >
        <template slot-scope="scope">
          <!-- <span>{{ parseTime(scope.row.commentTime, "{y}-{m}-{d} {h}:{m}:{s}") }}</span> -->
          <span>{{ scope.row.commentTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:evaluate:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:evaluate:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择用户id">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="综合评分" prop="star">
          <el-rate style="margin-top: 8px" v-model="form.star" :colors="colors">
          </el-rate>
        </el-form-item>
        <el-form-item label="评语" prop="comments">
          <el-input
            v-model="form.comments"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="评价照片" prop="photo">
          <image-upload v-model="form.imgList" />
        </el-form-item>
        <el-form-item label="评级时间" prop="commentTime">
          <el-date-picker
            clearable
            v-model="form.commentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择评级时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEvaluate,
  getEvaluate,
  delEvaluate,
  addEvaluate,
  updateEvaluate,
} from "@/api/order/evaluate";

export default {
  name: "Evaluate",
  dicts: ["sys_user_sex", "order_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单评价表格数据
      evaluateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 评级时间时间范围
      daterangeCommentTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderId: null,
        star: null,
        commentTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "change" },
        ],
        orderId: [
          { required: true, message: "订单id不能为空", trigger: "blur" },
        ],
        star: [
          { required: true, message: "综合评分不能为空", trigger: "blur" },
        ],
      },

      // >>>>>>>>>>>>>
      colors: ["#99A9BF", "#F7BA2A", "#FF9900"],
      oldImgList: null,
      //<<<<<<<<<<<<<
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单评价列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeCommentTime &&
        "" != this.daterangeCommentTime
      ) {
        this.queryParams.params["beginCommentTime"] =
          this.daterangeCommentTime[0];
        this.queryParams.params["endCommentTime"] =
          this.daterangeCommentTime[1];
      }
      listEvaluate(this.queryParams).then((response) => {
        this.evaluateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderId: null,
        star: null,
        comments: null,
        commentTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCommentTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单评价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEvaluate(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单评价";

        //验证照片是否改变
        this.oldImgList = this.form.imgList;
      });
    },

    // 判断是否修改图片
    changeImg() {
      if (this.form.imgList != this.oldImgList) {
        this.form.isImgChanged = true;
      }
      console.log("照片改变状态：" + this.form.isImgChanged);
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            // 判断是否改变照片
            this.changeImg();

            updateEvaluate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvaluate(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单评价编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEvaluate(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "order/evaluate/export",
        {
          ...this.queryParams,
        },
        `evaluate_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.hidden {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.hidden:hover {
  color: rgb(76, 130, 255);
  cursor: pointer;
  text-decoration: underline;
}
.flex{
  display: flex;
  align-items: center;
}
</style>